package com.wj.jiangan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wj.jiangan.pojo.*;
import com.wj.jiangan.pojo.vo.ClientVo;
import com.wj.jiangan.pojo.vo.OrderVo;
import com.wj.jiangan.service.IClientService;
import com.wj.jiangan.service.IEmpService;
import com.wj.jiangan.service.IOrderService;
import com.wj.jiangan.service.IPartService;
import com.wj.jiangan.service.impl.PartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wj
 * @since 2019-11-05
 */
@RestController
@RequestMapping("/Client")
public class ClientController{
	@Autowired
	private IOrderService orderService;
	@Autowired
	private IClientService clientService;
	private Map<String,Object> map = new HashMap<String,Object>();


	/**
	 * 根据员工id查询客户列表
	 *
	 * @param eid
	 * @return
	 * @创建人wj
	 */
	@GetMapping("{eid}")
	public List<Client> findClientByEid(@PathVariable int eid){
		return clientService.findClientByEid(eid);
	}

	/**
	 * @描述 查询客户信息
	 * @创建人 dt
	 * @创建时间 2019/11/6
	 */
	@GetMapping("/selectAll")
	public Map<String,Object> findAllClient(){

		List<Client> clients = clientService.querAllClien();
		map.put("code", 0);
		map.put("message", "");
		map.put("total", clients.size());
		map.put("data", clients);
		return map;
	}

	/**
	 * @param page
	 * @param pid  部门id
	 * @return
	 * @描述 三级联动
	 */
	@GetMapping("/select")
	public Map<String,Object> queryClientByPage(
			Page page,
			@RequestParam(required = false) Integer pid,
			@RequestParam(required = false) Integer eid,
			@RequestParam(required = false) Integer cid){
		Map<String,Object> param = new HashMap<>();
		param.put("pid", pid);
		param.put("eid", eid);
		param.put("cid", cid);

		IPage<ClientVo> clientVoIPage = clientService.findClientByPage(page, param);
		List<Map<String,Object>> list = new ArrayList<>();
		clientVoIPage.getRecords().forEach(o->{
			HashMap<String,Object> map = new HashMap<>();
			map.put("cid", o.getClient().getCId());
			map.put("cname", o.getClient().getCName());
			map.put("ctel", o.getClient().getCTel());
			map.put("caddress", o.getClient().getCAddress());
			map.put("cis", o.getClient().getCIs());
			list.add(map);
		});

		Map<String,Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("count", clientVoIPage.getTotal());
		result.put("data", list);

		return result;
	}


	/**
	 * @描述 分页查询客户表
	 * @创建人 dt
	 * @创建时间 2019/11/8
	 */
	@GetMapping("/findClientPage")
	public Map<String,Object> findpageClientNum(Page<Client> page, Integer cis){

		IPage<Client> clientByPage = clientService.findClientByPage(page, cis);

		List<Map<String,Object>> list = new ArrayList<>();


		clientByPage.getRecords().forEach(o->{
			HashMap<String,Object> map = new HashMap<>();
			map.put("cid", o.getCId());
			map.put("cis", o.getCIs());
			map.put("cname", o.getCName());
			map.put("ctel", o.getCTel());
			map.put("caddress", o.getCAddress());
			list.add(map);
		});
		Map<String,Object> result = new HashMap<>();
		result.put("cis", cis);
		result.put("code", 0);
		result.put("message", "");
		result.put("count", clientByPage.getTotal());
		result.put("data", list);
		return result;

	}


	/**
	 * @描述 添加客户信息
	 * @创建人 dt
	 * @创建时间 2019/11/6
	 */
	@RequestMapping("/insert")
	@ResponseStatus(HttpStatus.CREATED)
	public Map saveClient(Optional<Client> client){
		System.err.println(client);
		clientService.addAllClien(client.orElseThrow(IllegalStateException::new));
		map.put("code", 0);
		return map;
	}

	/**
	 * @描述 修改客户信息
	 * @创建人 dt
	 * @创建时间 2019/11/6
	 */
	@RequestMapping("/update")
	public Map<String,Object> updateClient(Client client){
		clientService.updateAllClien(client);
		map.put("code", 0);
		return map;
	}

	/**
	 * @描述 删除操作
	 * @创建人 dt
	 * @创建时间 2019/11/14
	 */
	@RequestMapping("/updatecIsBycId")
	public Map updatePIsByPId(Client client){
		map.put("code", 0);
		//删除部门

		if(client.getCIs() == 1){
			List<Object> order = orderService.queryOrderByid(client.getCId());
			if(order.size() > 0){
				map.put("ct", 1);//返回1表示客户下存在订单
				return map;
			}else{
				client.setCIs(0);
				QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<>();
				clientQueryWrapper.eq("c_id", client.getCId());
				clientService.updateClient(client, clientQueryWrapper);
				map.put("ct", 0);//返回0表示删除成功
				return map;
			}
		}
		return map;
	}
}







